package sg.edu.nus.iss.vmcs.system;

import sg.edu.nus.iss.vmcs.store.CashStoreItem;
import sg.edu.nus.iss.vmcs.store.Coin;
import sg.edu.nus.iss.vmcs.store.StoreItem;

/** 
 * @version 1.0
 * @author Low Chen Wei
 */

public class CashLoader implements ItemLoader {
	
	private static final String PROP_NUM_ITEMS = "NumOfItems";
	private static final String NAME_LABEL     = "Name";
	private static final String WEIGHT_LABEL   = "Weight";
	private static final String VALUE_LABEL    = "Value";
	private static final String QUANTITY_LABEL = "Quantity";
	
	private PropertyImp propImp;
	
	public CashLoader(PropertyImp propImp) {
		this.propImp = propImp;
	}

	public StoreItem getItem(Integer identifier) {
		int idx = identifier.intValue() + 1;
		Coin coin = new Coin();

		String value = propImp.getValue(String.valueOf(idx), NAME_LABEL).toString();
		coin.setName(value);

		value = propImp.getValue(String.valueOf(idx), WEIGHT_LABEL).toString();
		coin.setWeight(Double.parseDouble(value));

		value = propImp.getValue(String.valueOf(idx), VALUE_LABEL).toString();
		coin.setValue(Integer.parseInt(value));

		value = propImp.getValue(String.valueOf(idx), QUANTITY_LABEL).toString();
		int qty = Integer.parseInt(value);

		CashStoreItem item = new CashStoreItem(coin, qty);
		return item;
	}

	public void setItem(Integer index, StoreItem item) {
		int idx = index.intValue() + 1;

		CashStoreItem cashItem = (CashStoreItem) item;
		Coin cn = (Coin) cashItem.getContent();

		propImp.setValue(String.valueOf(idx), NAME_LABEL, cn.getName());
		propImp.setValue(String.valueOf(idx), WEIGHT_LABEL, cn.getWeight());
		propImp.setValue(String.valueOf(idx), VALUE_LABEL, cn.getValue());
		propImp.setValue(String.valueOf(idx), QUANTITY_LABEL, String.valueOf(cashItem.getQuantity()));	
	}
	
	public Integer getNumOfItems() {
		String nm = propImp.getValue(PROP_NUM_ITEMS).toString();
		int nmi;
		nmi = Integer.parseInt(nm);
		return nmi;
	}
	
	public void setNumOfItems(Integer items) {
		propImp.setValue(PROP_NUM_ITEMS, String.valueOf(items));
	}
	
}
